//
//  YHAddPartViewController.swift
//  SortProject
//
//  Created by ALH000355 on 2022/8/29.
//

import UIKit
// 左侧tableView，右侧自定义collectionView
// https://blog.csdn.net/potato512/article/details/107433546?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-107433546-blog-103522747.pc_relevant_vip_default&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-1-107433546-blog-103522747.pc_relevant_vip_default&utm_relevant_index=1
// https://juejin.cn/post/6944994974614323213
// https://juejin.cn/post/6854573215025463310
class YHAddPartViewController: BaseViewController {

    var waterView: UICollectionView!
    var flowView: UICollectionView!
    let titleList = ["布加迪威龙", "林肯", "理想One", "雪佛兰", "广汽本田", "丰田", "宝马", "奥迪", "布加迪威龙", "林肯", "理想One", "雪佛兰", "广汽本田", "丰田", "宝马", "奥迪"]
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        let margin = 3.0
        // 设置 flowlayout
        let wlayout = YHWaterFallFlowLayout()
        wlayout.minimumLineSpacing = margin
        wlayout.minimumInteritemSpacing = margin
        wlayout.sectionInset = UIEdgeInsets(top: 0, left: margin, bottom: 0, right: margin)
        wlayout.delegate = self
        waterView = UICollectionView.init(frame: CGRect(x: 20, y: 100, width: 200, height: 100), collectionViewLayout: wlayout)
        waterView.delegate = self
        waterView.dataSource = self
        self.view.addSubview(waterView)
        waterView.register(YHPartCell.self, forCellWithReuseIdentifier: "YHPartCell")
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        layout.sectionInset = UIEdgeInsets(top: 0, left: margin, bottom: 0, right: margin)
        layout.itemSize = CGSize(width: 50, height: 26)
        flowView = UICollectionView.init(frame: CGRect(x: 20, y: 430, width: 200, height: 300), collectionViewLayout: layout)
        flowView.delegate = self
        flowView.dataSource = self
        self.view.addSubview(flowView)
        flowView.register(YHPartCell.self, forCellWithReuseIdentifier: "YHPartCell")
    }
    
}
extension YHAddPartViewController: YHWaterFallLayoutDelegate{
    func waterFlowLayout(_ waterFlowLayout: YHWaterFallFlowLayout, itemWidth indexPath: IndexPath) -> CGFloat {
        let title = titleList[indexPath.row]
        let dic = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)]
        let size = (title as NSString).boundingRect(with: CGSize(width: 200, height: 20), options: [.truncatesLastVisibleLine, .usesLineFragmentOrigin], attributes: dic, context: nil).size
        return CGFloat(size.width + 10)
    }
}
extension YHAddPartViewController: UICollectionViewDelegate {
    
}
extension YHAddPartViewController: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return titleList.count
    }
   
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YHPartCell", for: indexPath) as! YHPartCell
        let title = titleList[indexPath.row]
        cell.title = title
        return cell
    }
}
